<template>
  <BaseLayout @view-change="handleViewChange">
    <!-- 左侧边栏 -->
    <div class="sidebar">
      <template v-if="currentView === 'chat'">
        <!-- 搜索框 -->
        <div class="search-box">
          <i class="ri-search-line"></i>
          <input type="text" placeholder="搜索联系人" v-model="searchQuery">
        </div>

        <!-- 联系人列表 -->
        <div class="contact-list">
          <div 
            v-for="contact in contacts" 
            :key="contact.id"
            :class="['contact-item', { active: currentChat === contact.id }]"
            @click="selectChat(contact.id)"
          >
            <img :src="contact.avatar" alt="avatar" class="contact-avatar">
            <div class="contact-info">
              <div class="contact-name">{{ contact.name }}</div>
              <div class="last-message">{{ contact.lastMessage }}</div>
            </div>
            <div class="contact-meta">
              <div class="time">{{ contact.lastTime }}</div>
              <div v-if="contact.unread" class="unread-count">{{ contact.unread }}</div>
            </div>
          </div>
        </div>
      </template>
      
      <template v-else-if="currentView === 'contacts'">
        <AddressBook />
      </template>

      <template v-else-if="currentView === 'favorites'">
        <Favorites />
      </template>

      <template v-else-if="currentView === 'files'">
        <FileManager />
      </template>

      <template v-else-if="currentView === 'settings'">
        <Settings />
      </template>
    </div>

    <!-- 右侧聊天区域 -->
    <div class="chat-area">
      <template v-if="currentChat">
        <!-- 聊天头部 -->
        <div class="chat-header">
          <div class="contact-info">
            <img :src="getCurrentContact.avatar" alt="avatar" class="contact-avatar">
            <div class="contact-details">
              <div class="contact-name">{{ getCurrentContact.name }}</div>
              <div class="contact-status">在线</div>
            </div>
          </div>
          <div class="header-actions">
            <i class="ri-phone-line"></i>
            <i class="ri-video-line"></i>
            <i class="ri-more-2-line"></i>
          </div>
        </div>

        <!-- 消息列表 -->
        <div class="message-list" ref="messageList">
          <div 
            v-for="message in messages" 
            :key="message.id"
            :class="['message', { 'message-mine': message.isMine }]"
          >
            <img :src="message.avatar" alt="avatar" class="message-avatar">
            <div class="message-content">
              <div class="message-text">{{ message.text }}</div>
              <div class="message-time">{{ message.time }}</div>
            </div>
          </div>
        </div>

        <!-- 输入区域 -->
        <div class="chat-input">
          <div class="input-actions">
            <i class="ri-emotion-line"></i>
            <i class="ri-image-line"></i>
            <i class="ri-file-line"></i>
          </div>
          <input 
            type="text" 
            v-model="messageText" 
            @keyup.enter="sendMessage"
            placeholder="输入消息..."
          >
          <button class="send-btn" @click="sendMessage">
            <i class="ri-send-plane-line"></i>
          </button>
        </div>
      </template>

      <!-- 未选择聊天时的提示 -->
      <div v-else class="no-chat-selected">
        <img src="../assets/chat-logo.png" alt="logo" class="welcome-logo">
        <h2>欢迎使用 W-IM</h2>
        <p>选择一个联系人开始聊天</p>
      </div>
    </div>
  </BaseLayout>
</template>

<script setup>
import { ref, computed } from 'vue'
import BaseLayout from '../layouts/BaseLayout.vue'
import AddressBook from './AddressBook.vue'
import Favorites from './Favorites.vue'
import FileManager from './FileManager.vue'
import Settings from './Settings.vue'
import defaultAvatar from '../assets/images/avatars/default.svg'

// 当前用户
const currentUser = ref('Admin')

// 使用 DiceBear 生成的头像 URL
const avatarUrl = defaultAvatar
const defaultContactAvatar = defaultAvatar

// 搜索查询
const searchQuery = ref('')

// 当前选中的聊天
const currentChat = ref(null)

// 消息输入
const messageText = ref('')

// 修改联系人数据中的头像
const contacts = ref([
  {
    id: 1,
    name: '张三',
    avatar: defaultAvatar,
    lastMessage: '好的，明天见！',
    lastTime: '10:30',
    unread: 2
  },
  {
    id: 2,
    name: '李四',
    avatar: defaultAvatar,
    lastMessage: '项目进展如何？',
    lastTime: '昨天',
    unread: 0
  },
  // 可以添加更多联系人
])

// 修改消息数据中的头像
const messages = ref([
  {
    id: 1,
    text: '你好！',
    time: '10:00',
    isMine: false,
    avatar: avatarUrl
  },
  {
    id: 2,
    text: '你好，最近怎么样？',
    time: '10:01',
    isMine: true,
    avatar: avatarUrl
  },
  // 可以添加更多消息
])

// 获取当前联系人信息
const getCurrentContact = computed(() => {
  return contacts.value.find(contact => contact.id === currentChat.value) || {}
})

// 选择聊天
const selectChat = (contactId) => {
  currentChat.value = contactId
}

// 发送消息
const sendMessage = () => {
  if (!messageText.value.trim()) return

  messages.value.push({
    id: Date.now(),
    text: messageText.value,
    time: new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
    isMine: true,
    avatar: avatarUrl
  })

  messageText.value = ''
  // 这里可以添加发送消息到服务器的逻辑
}

// 添加视图状态
const currentView = ref('chat')

// 视图切换处理
const handleViewChange = (view) => {
  currentView.value = view
  if (view !== 'chat') {
    currentChat.value = null
  }
}

// 添加收藏消息功能
const addToFavorites = (message) => {
  // 实现添加到收藏的逻辑
  console.log('Adding to favorites:', message)
}
</script>

<style scoped>
.sidebar {
  width: 300px;
  height: 100%;
  background: white;
  border-right: 1px solid #edf2f7;
  display: flex;
  flex-direction: column;
}

.search-box {
  padding: 20px;
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: none;
  background: #f1f5f9;
  border-radius: 8px;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.search-box i {
  position: absolute;
  left: 35px;
  top: 50%;
  transform: translateY(-50%);
  color: #64748b;
}

.search-box input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(161, 140, 209, 0.2);
}

.contact-list {
  flex: 1;
  overflow-y: auto;
}

.contact-item {
  padding: 15px 20px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.contact-item:hover {
  background: rgba(161, 140, 209, 0.1);
}

.contact-item.active {
  background: rgba(161, 140, 209, 0.15);
}

.contact-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  margin-right: 15px;
}

.contact-info {
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 4px;
}

.last-message {
  color: #64748b;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-meta {
  text-align: right;
  min-width: 60px;
}

.time {
  color: #94a3b8;
  font-size: 0.8rem;
  margin-bottom: 4px;
}

.unread-count {
  background: #a18cd1;
  color: white;
  font-size: 0.8rem;
  padding: 2px 6px;
  border-radius: 10px;
  display: inline-block;
}

.chat-area {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: white;
  position: relative;
}

.chat-header {
  padding: 20px;
  border-bottom: 1px solid #edf2f7;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.contact-info {
  display: flex;
  align-items: center;
}

.contact-details {
  margin-left: 15px;
}

.contact-name {
  font-weight: 600;
  color: #1e293b;
}

.contact-status {
  color: #10b981;
  font-size: 0.9rem;
}

.header-actions {
  display: flex;
  gap: 20px;
}

.header-actions i {
  font-size: 1.2rem;
  color: #64748b;
  cursor: pointer;
  transition: color 0.3s ease;
}

.header-actions i:hover {
  color: #a18cd1;
}

.message-list {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #f8fafc;
}

.message {
  display: flex;
  align-items: flex-start;
  margin-bottom: 20px;
}

.message-mine {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin: 0 10px;
}

.message-content {
  max-width: 60%;
}

.message-text {
  padding: 12px 16px;
  border-radius: 12px;
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin-bottom: 4px;
}

.message-mine .message-text {
  background: linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%);
  color: white;
}

.message-time {
  font-size: 0.8rem;
  color: #94a3b8;
  text-align: right;
}

.chat-input {
  padding: 20px;
  border-top: 1px solid #edf2f7;
  display: flex;
  align-items: center;
  gap: 15px;
  background: white;
}

.input-actions {
  display: flex;
  gap: 15px;
}

.input-actions i {
  font-size: 1.2rem;
  color: #64748b;
  cursor: pointer;
  transition: color 0.3s ease;
}

.input-actions i:hover {
  color: #a18cd1;
}

.chat-input input {
  flex: 1;
  padding: 10px 15px;
  border: none;
  background: #f1f5f9;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
}

.chat-input input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(161, 140, 209, 0.2);
}

.send-btn {
  background: linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.send-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(161, 140, 209, 0.4);
}

.no-chat-selected {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #64748b;
}

.welcome-logo {
  width: 120px;
  height: 120px;
  margin-bottom: 20px;
  opacity: 0.8;
}
</style> 